package com.flyox.game.militarychess.service.astar;

import com.flyox.game.militarychess.CONSTS;
import com.flyox.game.militarychess.bean.ChessMan;
import com.flyox.game.militarychess.bean.ChessMan.NAME;
import com.flyox.game.militarychess.service.ChessService;
import com.flyox.game.militarychess.service.CommonLayoutService;

public class PosBean implements Comparable<Object> {
	int row, col;// 棋子布局坐标
	
	ChessService cs=new ChessService();
	int[][] ps=new CommonLayoutService().getPosProperties();
	ChessMan[][] layout;
	
	// 方向
	private boolean up = false;// 是否搜寻过 true=搜寻过 false=未搜寻过
	private boolean down = false;
	private boolean left = false;
	private boolean right = false;

	public boolean isNullPos(){
		if(row==6&&(col==1||col==3||col==6||col==8||col==11||col==13||col==16||col==18)){
			return true;
		}else{
			return false;
		}
	}
	public boolean isNullPos(int row,int col){
		if(row==6&&(col==1||col==3||col==6||col==8||col==11||col==13||col==16||col==18)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 检查这个位置是否有棋子
	 * 
	 * @param row
	 * @param col
	 * @return
	 */
	private boolean canMoveHere(int row, int col) {
		if (layout == null || (layout[row][col] != null && !layout[row][col].getName().equals(NAME.EMPTY))) {
			return false;
		} else if((ps[row][col]&CONSTS.POS_RAINWAY)!=CONSTS.POS_RAINWAY){//不是铁路线不能到达
			return false;
		}else{
			return true;
		}
	}

	public boolean canMoveUp(PosBean endPos) {
		if (up) {
			return false;
		} else {
			up=true;
			if((row-1)==endPos.row&&col==endPos.col){
				return true;
			}else if (row - 1 < 1) {// 最后一行不能 行走
				return false;
			} else if (!canMoveHere(row - 1, col)) {
				return false;
			} else {
				return true;
			}
		}
	}

	public boolean canMoveDown(PosBean endPos) {
		if (down) {
			return false;
		} else {
			down=true;
			if((row+1)==endPos.row&&col==endPos.col){
				return true;
			}else if (row + 1 > 11) {// 最后一行不能 行走
				return false;
			} else if (!canMoveHere(row + 1, col)) {
				return false;
			} else {
				return true;
			}
		}
	}

	public boolean canMoveLeft(PosBean endPos) {
		int curCol=col;
		if(isNullPos(row,col-1)){
			curCol-=2;
		}else{
			curCol--;
		}
		
		if (left) {
			return false;
		} else {
			left=true;
			if(row==endPos.row&&curCol==endPos.col){
				return true;
			}else if (curCol < 0) {
				return false;
			}else if (!canMoveHere(row, curCol)) {
				return false;
			} else {
				return true;
			}
		}
	}

	public boolean canMoveRight(PosBean endPos) {
		int curCol=col;
		if(isNullPos(row,col+1)){
			curCol+=2;
		}else{
			curCol++;
		}
		
		if (right) {
			return false;
		} else {
			right=true;
			if(row==endPos.row&&curCol==endPos.col){
				return true;
			}else if (curCol > 19) {// 最后一行不能 行走
				return false;
			} else if (!canMoveHere(row , curCol)) {
				return false;
			} else {
				return true;
			}
		}
	}

	public PosBean(int row, int col) {
		this.row = row;
		this.col = col;
	}

	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public int getCol() {
		return col;
	}

	public void setCol(int col) {
		this.col = col;
	}

	public String toString() {
		return "" + row + "_" + col;
	}

	public int compareTo(Object o) {
		if (o instanceof PosBean) {
			PosBean pb = (PosBean) o;
			if (pb.row == getRow() || pb.col == getCol()) {
				return 0;
			} else {
				return -1;
			}
		} else {
			return -1;
		}
	}
	public void setLayout(ChessMan[][] layout) {
		this.layout = layout;
	}

}
